export default {
	openFile(url) {
		uni.showLoading({
			title: '加载中'
		});
		setTimeout(() => {
			uni.hideLoading()
		}, 1000 * 60 * 2)
		uni.downloadFile({
			url,
			success: (res) => {
				uni.hideLoading()
				if (res.statusCode === 200) {
					var filePath = res.tempFilePath;
					uni.openDocument({
						filePath: filePath,
						showMenu: true,
						success: function(res) {
							//console.log('打开文档成功');
						}
					});
				}
			}
		});
	},


	/**
	 * 跳转再封装,主要是为了兼容外链。
	 * @param {String} path - 跳转路径
	 * @param {isTabbar} isTabbar - 是否是底部导航
	 */
	routerTo(path = '', params = {}, isTabbar = false) {
		// console.log(path);
		let url = path
		if (!path) {
			uni.$u.toast("跳转开发中")
			return
		}
		if (!uni.$u.test.isEmpty(params)) {
			// queryParams 对象转URL参数 拼接成新的地址
			url = path + uni.$u.queryParams(params)
		}
		// if(isTabbar)
		uni.navigateTo({
			url,
			fail(error) {
				console.log(error);
			}
		});
	},
	/**
	 * 图片处理-预览图片
	 * @param {Array} urls - 图片列表
	 * @param {Number} current - 首个预览下标
	 */
	previewImage(urls = [], current = 0) {
		uni.previewImage({
			urls: urls,
			current: current,
			indicator: 'default',
			loop: true,
			fail(err) {
				console.log('previewImage出错', urls, err)
			},
		})
	},

	copyText(data) {
		// 复制
		uni.setClipboardData({
			data, // 复制的内容 
			success: function() {
				console.log('success');
				uni.showToast({
					title: '复制成功',
					duration: 2000
				})
			}
		});
	},

	dealNumber(money) {
		if (money && money != null) {
			money = String(money);
			let left = money.split('.')[0],
				right = money.split('.')[1];
			right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
			let temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
			// 这行是 ， 和 . 的组合形式
			// return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
			return (Number(money) < 0 ? "-" : "") + temp.join('.').split('').reverse().join('') + right;
		} else if (money === 0) {
			return '0.00';
		} else {
			return "";
		}
	},

	// 数字转汉字
	toChinese(num) {
		num = Math.floor(num);
		var chinese = "";
		var digits = Math.floor(Math.log10(num)) + 1;
		var digit = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
		var times = ['', '十', '百', '千', '万'];
		if (digits >= 9) {
			var quotient = Math.floor(num / Math.pow(10, 8));
			var remainder = num % Math.pow(10, 8);
			var remainderDigits = Math.floor(Math.log10(remainder)) + 1;
			return toChinese(quotient) + '亿' + (remainderDigits < 8 ? "零" : "") + (remainder > 0 ? toChinese(
				remainder) : "");
		}
		if (digits == 1) return digit[num];
		if (digits == 2) {
			var quotient = Math.floor(num / 10);
			var remainder = num % 10;
			if (quotient > 1) {
				chinese = digit[quotient];
			}
			chinese = chinese + times[1];
			if (remainder > 0) {
				chinese = chinese + digit[remainder];
			}
			return chinese;
		}
		if (digits > 5) {
			var quotient = num / Math.pow(10, 4);
			var remainder = num % Math.pow(10, 4);
			var remainderDigits = Math.floor(Math.log10(remainder)) + 1;
			return toChinese(quotient) + '万' + (remainderDigits < 4 ? "零" : "") + (remainder > 0 ? toChinese(
				remainder) : "");
		}
		for (var index = digits; index >= 1; index--) {
			var number = Math.floor(num / Math.pow(10, index - 1) % 10);
			//console.log(index+" "+number);
			if (number > 0) {
				chinese = chinese + digit[number] + times[index - 1];
			} else {
				if (index > 1) {
					var nextNumber = Math.floor(num / Math.pow(10, index - 2) % 10);
					if (nextNumber > 0 && index > 1) {
						chinese = chinese + digit[number];
					}
				}

			}
		}
		return chinese;
	}

}